package com.cai.xiaobai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cai.xiaobai.entity.OptionInfo;
import com.cai.xiaobai.entity.VO.fromInfo.getFromInfo.OptionInfoVO;
import com.cai.xiaobai.mapper.OptionInfoMapper;
import com.cai.xiaobai.service.IOptionInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 选项内容表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-05-13
 */
@Service
public class OptionInfoServiceImpl extends ServiceImpl<OptionInfoMapper, OptionInfo> implements IOptionInfoService {

    @Override
    public Map<Long, List<OptionInfoVO>> getOptionMap(List<Long> questionInfoList) {
        List<OptionInfo> list = this.list(new LambdaQueryWrapper<OptionInfo>().in(OptionInfo::getQuestionId, questionInfoList));
        Map<Long, List<OptionInfoVO>> map = new HashMap<>();
        list.forEach(optionInfo -> {
            if (Objects.isNull(map.get(optionInfo.getQuestionId()))) {
                map.put(optionInfo.getQuestionId(), new ArrayList<>());
            }
            List<OptionInfoVO> vos = map.get(optionInfo.getQuestionId());
            OptionInfoVO optionInfoVO = new OptionInfoVO();
            BeanUtils.copyProperties(optionInfo, optionInfoVO);
            vos.add(optionInfoVO);
        });
        for (Map.Entry<Long, List<OptionInfoVO>> entry : map.entrySet()) {
            List<OptionInfoVO> collect = entry.getValue().stream()
                .sorted(Comparator.comparing(OptionInfoVO::getOptionSort)).toList();
            map.put(entry.getKey(), collect);
        }
        return map;
    }
}
